package com.yun.blog.controller;

import com.yun.blog.bean.LoginBean;
import com.yun.blog.bean.RegisterBean;
import com.yun.blog.bean.Result;
import com.yun.blog.service.LoginService;
import com.yun.blog.service.impl.OauthContext;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @author : simple
 * @title : LoginController
 * @projectName : dantouBlog-back
 * @description : 登录接口
 * @email : yun_simple@163.com
 * @date : 2023/5/21下午8:29
 */

@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Resource
    LoginService loginService;

    @Resource
    OauthContext oauthContext;

    @PostMapping("/login")
    @ApiOperation("用户登录接口")
    public Result login(@RequestBody LoginBean loginBean, HttpSession session) {
        Result success = Result.success();
        success.setMsg("登录成功！");
        HashMap<String, Object> dataMap = loginService.login(loginBean, session);
        success.setDataMap(dataMap);
        return success;
    }

    @PostMapping("/register")
    @ApiOperation("用户注册接口")
    public Result register(@RequestBody RegisterBean registerBean, HttpSession session) {
        loginService.register(registerBean, session);
        return Result.success("注册成功");
    }


    /**
     * 获取第三方认证页面的地址
     *
     * @param otherType qq | gitee
     * @return Result
     * @author yun
     * @date 2023/5/21 下午9:29
     */
    @GetMapping("/getOauthUrl")
    public Result getOauthUrl(String otherType) {
        String url = oauthContext.get(otherType + "Oauth").getOauthUrl();
        return Result.success().add("url", url);
    }


    /**
     * 其他第三方登录
     *
     * @param otherType 登录方式
     * @param code      code
     * @return Result
     * @author yun
     * @date 2023/6/5 下午5:20
     */
    @GetMapping("/otherLogin")
    public Result otherLogin(String otherType, String code) {
        HashMap<String, Object> data = oauthContext.get(otherType + "Oauth").login(code);
        Result success = Result.success();
        success.setDataMap(data);
        return success;
    }


}
